﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace Zhaoxi.AdvancedCourse.Attrs
{
    public static class RemarkAttrHelper
    {
        /// <summary>
        /// 获取指定类型的Remark描述文本
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public static string GetClassDescription<T>()
        {
            string remark = null;
            Type type = typeof(T);
            RemarkAttribute remarkAttr= type.GetCustomAttribute<RemarkAttribute>();
            if(remarkAttr != null)
            {
                remark = remarkAttr.Description;
            }
            return remark;
        }

        public static string GetClassDescription(this Type type)
        {
            string remark = null;
            RemarkAttribute remarkAttr = type.GetCustomAttribute<RemarkAttribute>();
            if (remarkAttr != null)
            {
                remark = remarkAttr.Description;
            }
            return remark;
        }

        /// <summary>
        /// 获取指定属性的描述文本
        /// </summary>
        /// <param name="pro"></param>
        /// <returns></returns>
        public static string GetPropertyDescription(this PropertyInfo pro)
        {
            string remark = null;
            RemarkAttribute remarkAttr = pro.GetCustomAttribute<RemarkAttribute>();
            if (remarkAttr != null)
            {
                remark = remarkAttr.Description;
            }
            else
                remark = pro.Name;
            return remark;
        }

        public static string GetObjectInfo<T>(T obj)
        {
            string result = "";
            var type = typeof(T);
            var props= type.GetProperties();
            foreach( var prop in props)
            {
                string remark = prop.GetPropertyDescription();
                if (result != "")
                    result += ",";
                result += remark + " : " + prop.GetValue(obj);
            }
            return result;
        }
    }
}
